<!--
    Powered By nodePPT - This is probably the best web presentation tool so far!
    version: 0.9.8-3
    site: https://github.com/ksky521/nodePPT
-->
<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>python_04_文件 - By woniuppp</title>
    <link rel="stylesheet" media="all" href="./css/nodeppt2.0.css">
    <link rel="stylesheet" media="only screen and (max-device-width: 480px)" href="./css/phone.css">
    <link rel="stylesheet" media="print" href="./css/pdf.css">
    <link rel="stylesheet" href="./js/highlight/styles/monokai_sublime.css">
    <link rel="stylesheet" href="./css/font-awesome.css">
</head>
<body>
<div class="slides">
    <slides id="container">
        <slide class="slide"><section class="slide-wrapper"><article class="flexbox vcenter">
<h1>python基础教程<em>04</em>文件</h1>
<p><small>woniuppp</small></p>

</article></section></slide>
<slide class="slide"><section class="slide-wrapper"><hgroup>
<h2>上节回顾</h2>

</hgroup><article>

<ul>
<li>元组</li>
<li>按值和按索引取值的区别</li>
<li>字典的定义</li>
<li>字典的特点：没顺序，查找快，key不重复。。。</li>
<li>通用方法 len del  for in</li>
<li>in 判断元素</li>
<li>更新字典</li>
<li>字典方法:clear,copy,formkeys,get,popitem</li>
</ul>

</article></section></slide>
<slide class="slide"><section class="slide-wrapper"><hgroup>
<h2>上节回顾---String</h2>

</hgroup><article>

<ul>
<li>字符串的定义，取字符</li>
<li>切片</li>
<li>join &#39;|&#39;.join([&#39;hello&#39;,&#39;world&#39;])</li>
<li>find</li>
<li>index</li>
<li>capitalize</li>
<li>replace</li>
<li>split</li>
<li>热身：通过多个字符分隔</li>
<li>热身：数据替换，分隔符{}</li>
</ul>

</article></section></slide>
<slide class="slide"><section class="slide-wrapper"><hgroup>
<h2>复习一下上节任务，小练习</h2>

</hgroup><article>

<ul>
<li>取球（思路和代码）</li>
</ul>

</article></section></slide>
<slide class="slide"><section class="slide-wrapper"><hgroup>
<h2>可以搞点实际的东西了--文件操作</h2>

</hgroup><article>

<ul>
<li>打开文件 open</li>
<li>读文件 read readline readlines</li>
<li>写文件 write</li>
<li>关闭文件 close</li>
</ul>

</article></section></slide>
<slide class="slide"><section class="slide-wrapper"><article>
<ul>
<li>打开文件 open，传入文件路径</li>
<li>如果文件不存在，会报错<pre><code class="python">
  f = open('hello.txt')
  print f
</code></pre></li>
</ul>

</article></section></slide>
<slide class="slide"><section class="slide-wrapper"><article>
<ul>
<li>文件的模式</li>
<li>如果想修改文件内容，需要提供文件的模式<ul>
<li>读（默认值） ： r</li>
<li>写 ： w</li>
<li>追加： a</li>
</ul>
</li>
</ul>

</article></section></slide>
<slide class="slide"><section class="slide-wrapper"><hgroup>
<h2>读</h2>

</hgroup><article>

<pre><code class="python">
    f = open('demo.txt','r')
    print f.read(4)
    print f.read(2)
    print f.read()
    f.close()
</code></pre>
</article></section></slide>
<slide class="slide"><section class="slide-wrapper"><hgroup>
<h2>读一行，也可以传入参数，限制个数</h2>
<h3>尝试实现</h3>

</hgroup><article>

<pre><code class="python">
    f = open('demo.txt')
    print f.readline()
</code></pre>

<pre><code class="python">
    f = open('demo.txt')
    print f.readlines()
</code></pre>
</article></section></slide>
<slide class="slide"><section class="slide-wrapper"><hgroup>
<h2>写</h2>
<h3> </h3>

</hgroup><article>

<pre><code class="python">
    f = open('demo.txt','w')
    f.write('hello\n')
    f.write('world')
    f.close()
</code></pre>
</article></section></slide>
<slide class="slide"><section class="slide-wrapper"><hgroup>
<h2>写一行 writelines</h2>

</hgroup><article>

<pre><code class="python"> 
    f = open('demo.txt','w')
    f.writelines(['hello\n','world'])
    f.close()
</code></pre>
</article></section></slide>
<slide class="slide"><section class="slide-wrapper"><article>
<h2>追加模式</h2>
<h2>用a模式，练习一下之前的write例子，感受一下区别</h2>
<pre><code class="python">
    f = open('demo.txt','a')
    f.write('hello\n')
    f.write('world')
    f.close()
</code></pre>
</article></section></slide>
<slide class="slide"><section class="slide-wrapper"><article>
<h2>小练习</h2>
<ul>
<li>读取一个文件</li>
<li>请输出其内容。</li>
<li>请去除该文本的换行。</li>
<li>请替换其中的字符&quot;reboot&quot;为&quot;hello&quot;。</li>
<li>复制这个文件，把第二行内容换为&#39;wd&#39;</li>
</ul>

</article></section></slide>
<slide class="slide"><section class="slide-wrapper"><article>
<h2>小练习</h2>
<h3>tem.txt存储这日期和气温的数据，求最高温度和平局温度</h3>

</article></section></slide>
<slide class="slide"><section class="slide-wrapper"><article>
<h2>错误处理</h2>
<ul>
<li>代码出现异常是很常见的，比如open一个不存在的文件</li>
<li>我们需要控制异常，如果出错了，不能让程序崩溃、</li>
<li>我们可以手动抛出异常，定义自己的错误类型</li>
</ul>

</article></section></slide>
<slide class="slide"><section class="slide-wrapper"><article>
<h2>捕捉异常</h2>
<ul>
<li>try...except...else...finally<pre><code class="python">
  try:
      可能出错的代码
  except:
      如果出错
  else:
      如果不出错
  finaly:
      出不出错都执行
</code></pre></li>
</ul>

</article></section></slide>
<slide class="slide"><section class="slide-wrapper"><article>
<pre><code class="python">
    try:
        f = open('xx.txt')
    except:
        print 'file not exist'
    else:
        print 'file exist'
    finally:
        print 'always'
</code></pre>
</article></section></slide>
<slide class="slide"><section class="slide-wrapper"><article>
<h2>前端介绍</h2>
<h3>html+css+js</h3>
<ul>
<li>html负责展示内容</li>
<li>css负责展示样式</li>
<li>js负责动态效果</li>
</ul>

</article></section></slide>
<slide class="slide"><section class="slide-wrapper"><article>
<h2>整体前端代码介绍</h2>
<p><img src="../html.png" alt="Alt text"></p>

</article></section></slide>
<slide class="slide"><section class="slide-wrapper"><article>
<h2>html</h2>
<ul>
<li>都是标签</li>
<li>html</li>
<li>head<ul>
<li>title</li>
<li>meta</li>
</ul>
</li>
<li>body<ul>
<li>p</li>
<li>input<ul>
<li>checkbox</li>
<li>radio</li>
<li>passpord</li>
<li>button</li>
<li>text</li>
</ul>
</li>
</ul>
</li>
</ul>

</article></section></slide>
<slide class="slide"><section class="slide-wrapper"><article>
<h2>html标签可以有id属性，css通过这个id找到标签</h2>
<h3>css属性</h3>
<ul>
<li>width 宽度</li>
<li>height 高度</li>
<li>background 背景</li>
<li>float 浮动</li>
<li>margin-left左边距</li>
<li>margin-right右边距</li>
<li>margin-top上</li>
<li>margin-bottom下</li>
</ul>

</article></section></slide>
<slide class="slide"><section class="slide-wrapper"><article>
<h2>小练习</h2>
<h3>简单的nginx日志分析</h3>
<ul>
<li>日志文件在/home/shre/www_access_20140823.log</li>
<li>期望输出一个list，分别存储这http状态，访问url，ip，访问次数，如下图
<img src="../log.png" alt="Alt text"></li>
</ul>

</article></section></slide>
<slide class="slide"><section class="slide-wrapper"><hgroup>
<h2>微信公众号</h2>

</hgroup><article>

<p><img src="./logo.jpg" alt="reboot" title="reboot"></p>

</article></section></slide>
        <slide class="slide thank-you-slide segue nobackground">
            <article class="flexbox vleft auto-fadein">
                <h2>Q &amp; A</h2>
                <h3>&lt;Thank You!&gt;</h3>
            </article>
        </slide>
        <slide class="slide logoslide dark nobackground">
            <article class="flexbox vcenter">
              <h2 style="color: white;">Powered By nodePPT v0.9.8-3</h2>
            </article>
          </slide>
        <div class="slideTip" id="tip"></div>
    </slides>
</div>
<canvas id="drawBoard" class="draw-board" width="900" height="700"></canvas>
<div class="progress"><span id="progress"></span></div>
<div id="_buttons">
    <div class="_btn-box" id="_btn-box" style="display:none;">
        <button class="fa fa-arrow-circle-left" id="_btn-prev"></button>
        <button class="fa fa-arrow-circle-right" id="_btn-next"></button>
        <button class="fa fa-paint-brush" id="_btn-brush"></button>
        <button class="fa fa-compress" id="_btn-overview" data-toggle="fa fa-expand"></button>
    </div>
    <button class="fa fa-bars" id="_btn-bar"  data-toggle="fa fa-close"></button>
</div>
<script src="./js/mixjs/lib/mix.0.3.0.min.js"></script>
<script>
var base = location.protocol + '//' + location.host;

var path = location.pathname.split('/').filter(function(v){
    return !!v;
});
path.pop();
path = path.join('/');
MixJS.config({
    baseURL: [ base, path, 'js'].join('/')+'/'
});

</script>
<script src="./js/mixjs/lib/event/broadcast.js"></script>
<script src="./js/nodeppt.js"></script>
<script>
Slide.init({
    containerID: 'container',
    drawBoardID: 'drawBoard',
    slideClass: '.slide',
    buildClass: '.build',
    progressID: 'progress',
    transition: 'slide',
    width: 1100,
    dir: './',
    
    //打开下面的注释就开启postMessage方式
    //访问网址127.0.0.1:8080/ppt/demo#client
    control:{
        type: 'postMessage',
        args:{
            isControl:  false
        }
    },
    
    tipID: 'tip'
});
MixJS.loadJS('highlight/hljs-0.8.js',function(){
    hljs.tabReplace = '  ';
    hljs.initHighlightingOnLoad();
});
</script>
<script src="./js/demo.js"></script>
<link rel="stylesheet" href="./css/demo.css">
</body>
</html>
